package org.openmrs.module.basicmodule.web.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;

import org.openmrs.module.basicmodule.DSS.compiler.Compiler;


public class DWRDssFormEntryEvaluator {
                    
    public String evaluateForm(String input, String dssname) {
        String output=new String();
        System.out.println(System.getProperty("java.class.path"));
        System.out.println(System.getProperty("user.dir"));
        System.out.println(System.getProperty("user.home"));
    
                File mydir=new File("DSS_Programs");
        if (mydir.exists())
        {
            System.out.println("Exists");            
            
        }
           else
        {
            System.out.println("Not Exists");
            mydir.mkdir();
        }
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("temp_input.dss"), "utf-8"));
            writer.write(input);
        } catch (IOException ex){
        // report
        } finally {
            try {writer.close();} catch (Exception ex) {}
        }                
        //output=input+" : This is coming from the DWR Class for DSS evaluator. :  ";
        
            
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        // IMPORTANT: Save the old System.out!
        PrintStream old = System.out;
        // Tell Java to use your special stream
        System.setOut(ps);  
        // Print some output: goes to your special stream
        Compiler compiler = (new Compiler("temp_input.dss"));
        // Put things back
        System.out.flush();
        
        output+=compiler.compileProgram();
        output+="\n"+baos.toString();
        System.setOut(old);                    
        return output;
    }
//        File mydir=new File("DSS_Programs");
//        if (mydir.exists())
//        {
//            System.out.println("Exists");            
//            if(new File("DSS_Programs/"+dssname+".dss").exists())
//            {
//                System.out.println("Filename already exists");
//                return null;
//            }
//        }
                    
//    public String evaluateForm(String input) {
//        String output=new String();
//        System.out.println(System.getProperty("java.class.path"));
//        System.out.println(System.getProperty("user.dir"));
//        System.out.println(System.getProperty("user.home"));



//           else
//        {
//            System.out.println("Not Exists");
//            mydir.mkdir();
//        }
//        Writer writer = null;
//        try {
//            writer = new BufferedWriter(new OutputStreamWriter(
//            new FileOutputStream("DSS_Programs/"+dssname+".dss"), "utf-8"));
//            writer.write(input);
//        } catch (IOException ex){
//        // report
//        } finally {
//            try {writer.close();} catch (Exception ex) {}
//        }                
        //output=input+" : This is coming from the DWR Class for DSS evaluator. :  ";
        
            
     
    
    public String saveDSSForm(String input, String dssname){
        
        
        
        System.out.println(dssname);
        File mydir=new File("DSS_Programs");
        if (mydir.exists())
        {
            System.out.println("Exists");            
//            if(new File("DSS_Programs/"+dssname+".dss").exists())
//            {
//                System.out.println("Rule name already exists, please change the rule name");
//                return null;
//            }
        }
           else
        {
            System.out.println("Not Exists");
            mydir.mkdir();
        }
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream("DSS_Programs/"+dssname+".dss"), "utf-8"));
            writer.write(input);
        } catch (IOException ex){
        // report
        } finally {
            try {writer.close();} catch (Exception ex) {}
        }                
        
        
        
        
        
        return "Saved Successfully.";
    }
    
    public void activateDSS(String dssname) throws FileNotFoundException, IOException{
      
       File f = new File("DSS_Programs/"+dssname);
       FileReader fr = new FileReader(f);
       BufferedReader br = new BufferedReader(fr);
       
       File f2 = new File("activated.dss");
       FileWriter fw = new FileWriter(f2);
       PrintWriter pw = new PrintWriter(fw);
       
       while(true){
       String s = br.readLine();
       if(s==null){break;}
       else pw.println(s);
       }
       pw.close();
       
    }
    
    public String displaySelectedDSS(String dssname) throws FileNotFoundException, IOException
    {  String rule="";
       File f = new File("DSS_Programs/"+dssname);
       FileReader fr = new FileReader(f);
       BufferedReader br = new BufferedReader(fr);
       while(true){
       String s = br.readLine();
       if(s==null){break;}
       else 
       {rule+=s+"\n";}
           
       }
       
  
        return rule;
    }

    
    public boolean isRule(String dssname)
    {
        System.out.println(dssname);
            if(new File("DSS_Programs/"+dssname+".dss").exists())
            {
                   return true;
            }
            else return false;
        
    }
    

}
